package com.dj.mingkong.common.util;

import jakarta.servlet.http.HttpServletRequest;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.*;
import java.util.regex.Pattern;

public class IpUtils {
    private static final Pattern IP_PATTERN = Pattern.compile("([0-9]{1,3}\\.){3}[0-9]{1,3}");

    public IpUtils() {
    }

    private static String resolveClientIPFromXFF(String xff) {
        if (xff != null && xff.length() != 0) {
            String[] ss = xff.split(",");
            String[] var2 = ss;
            int var3 = ss.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                String ip = var2[var4];
                ip = ip.trim();
                if (isValidIP(ip)) {
                    return ip;
                }
            }

            return null;
        } else {
            return null;
        }
    }

    public static String iplongToIp(long ipaddress) {
        StringBuffer sb = new StringBuffer("");
        sb.append(String.valueOf(ipaddress >>> 24));
        sb.append(".");
        sb.append(String.valueOf((ipaddress & 16777215L) >>> 16));
        sb.append(".");
        sb.append(String.valueOf((ipaddress & 65535L) >>> 8));
        sb.append(".");
        sb.append(String.valueOf(ipaddress & 255L));
        return sb.toString();
    }

    public static long ipStrToLong(String ipaddress) {
        long[] ip = new long[4];
        int position1 = ipaddress.indexOf(".");
        int position2 = ipaddress.indexOf(".", position1 + 1);
        int position3 = ipaddress.indexOf(".", position2 + 1);
        ip[0] = Long.parseLong(ipaddress.substring(0, position1));
        ip[1] = Long.parseLong(ipaddress.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(ipaddress.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(ipaddress.substring(position3 + 1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    public static boolean isValidIP(String ip) {
        return ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip) ? IP_PATTERN.matcher(ip).matches() : false;
    }

    public static String getLocalIP() {
        Iterator var0 = getAllNoLoopbackAddresses().iterator();

        String a;
        do {
            if (!var0.hasNext()) {
                return "";
            }

            a = (String) var0.next();
        } while (!isValidIP(a));

        return a;
    }

    public static String getLocalAddress() {
        return LocalIpAddress.resolveLocalIp();
    }

    public static List<String> getLocalIPs() {
        List<String> localIps = new ArrayList();
        Iterator var1 = getAllNoLoopbackAddresses().iterator();

        while (var1.hasNext()) {
            String a = (String) var1.next();
            if (isValidIP(a)) {
                localIps.add(a);
            }
        }

        return localIps;
    }

    private static Collection<InetAddress> getAllHostAddress() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            ArrayList addresses = new ArrayList();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
                Enumeration inetAddresses = networkInterface.getInetAddresses();

                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
                    addresses.add(inetAddress);
                }
            }

            return addresses;
        } catch (Exception var5) {
            var5.printStackTrace();
            return null;
        }
    }

    private static Collection<String> getAllNoLoopbackAddresses() {
        Collection<String> noLoopbackAddresses = new ArrayList();
        Collection<InetAddress> allInetAddresses = getAllHostAddress();
        Iterator var2 = allInetAddresses.iterator();

        while (var2.hasNext()) {
            InetAddress address = (InetAddress) var2.next();
            if (!address.isLoopbackAddress()) {
                noLoopbackAddresses.add(address.getHostAddress());
            }
        }

        return noLoopbackAddresses;
    }

    public static String getIpAddress(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() ==0 || "nuknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
